package cn.south.campus.common.file.service;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author chow
 * @since 2023/3/21 19:41
 */
@Component
public class FileUploadFactory implements InitializingBean {

    @Resource
    private ApplicationContext applicationContext;

    private static final Map<String, IFileService> map = new HashMap<>();

    public static IFileService get(String type) {
        return map.get(type);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (!CollectionUtils.isEmpty(map)) {
            return;
        }
        String[] namesForType = applicationContext.getBeanNamesForType(IFileService.class);
        for (String s : namesForType) {
            IFileService fileService = (IFileService) applicationContext.getBean(s, IFileService.class);
            map.put(fileService.type(), fileService);
        }
    }

}
